package com.topnav.mc.transmit;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
/*import com.js.detection.gb28181.service.IGbServerService;
import com.js.detection.http.HttpUtil;
import com.js.detection.model.entity.GbServer;
import com.js.detection.model.entity.bean.GbChannelBean;
import com.js.detection.model.entity.bean.GbDeviceBean;*/
import com.js.lib.utils.JsonUtil;
import com.topnav.mc.admin.entity.bean.DeviceBean;
import com.topnav.mc.admin.entity.bean.DeviceChannelBean;
import com.topnav.mc.admin.service.IGbServerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;


@Component
@Slf4j
public class TransmitUtil {
    @Autowired
    private IGbServerService gbServerService;

    /**
     * 传输设备变更到国标平台
     * @param gbDeviceBeans
     * @param method 1更新 0删除
     */
    @Async("taskExecutor")
    public void transDevice(List<DeviceBean> gbDeviceBeans, Integer method){
/*
        // 如果有多个 GB SERVER
        List<Long> lists = new ArrayList<>();

        //Map<Integer,List<DeviceMsg>> serverMap = new HashMap<>();
        for(GbDeviceBean gbDeviceBean : gbDeviceBeans){
            if (!lists.contains(gbDeviceBean.getGbServerId())){
                lists.add(gbDeviceBean.getGbServerId());
            }
        }

        for(Long serverId: lists){
            LambdaQueryWrapper<GbServer> queryWrapper = new LambdaQueryWrapper();
            queryWrapper.eq(GbServer::getId,serverId);
            GbServer gbServer = gbServerService.getOne(queryWrapper,false);

            // 所有设备和通道推送给 国标平台
            List<DeviceMsg> deviceMsgs = new ArrayList<>();
            for(GbDeviceBean gbDeviceBean : gbDeviceBeans){
                deviceMsgs.clear();
                if (gbServer.getId().equals(gbDeviceBean.getGbDeviceId())){
                    // 组装成传输的数据
                    DeviceMsg deviceMsg = new DeviceMsg().setMethod(method)
                            .setDeviceInfo(new DeviceInfo()
                                    .setId(gbDeviceBean.getId())
                                    .setDeviceName(gbDeviceBean.getDeviceName())
                                    .setOriginDeviceId(gbDeviceBean.getGbDeviceId())
                                    .setNewDeviceId(gbDeviceBean.getGbDeviceId()));
                    deviceMsgs.add(deviceMsg);
                }
                // 设备列表发送到 国标平台

                if (deviceMsgs.size() > 0){
                    TransmitData transmitData = new TransmitData();
                    transmitData.setType("DEVICELIST");
                    transmitData.setData(deviceMsgs);
                    log.debug(JsonUtil.toJson(transmitData));
                    try{
                        HttpUtil.doPost(
                                "http://" + gbServer.getHost() + ":" + gbServer.getInterfacePort() + "/api/data/from",
                                JsonUtil.toJson(transmitData),
                                null,
                                30000
                        );
                    }catch (Exception e){
                        log.error(">>>>>>: {}",e.toString());
                    }
                }
            }
        }*/
    }

    /**
     * 传输设备变更到国标平台
     * @param gbChannelBeans
     * @param method
     */
    @Async("taskExecutor")
    public void transChannel(List<DeviceChannelBean> gbChannelBeans, Integer method){
/*
        // 如果有多个 GB SERVER
        List<Long> lists = new ArrayList<>();

        //Map<Integer,List<DeviceMsg>> serverMap = new HashMap<>();
        for(GbChannelBean gbChannelBean : gbChannelBeans){
            if (!lists.contains(gbChannelBean.getGbServerId())){
                lists.add(gbChannelBean.getGbServerId());
            }
        }

        for(Long serverId: lists){
            LambdaQueryWrapper<GbServer> queryWrapper = new LambdaQueryWrapper();
            queryWrapper.eq(GbServer::getId,serverId);
            GbServer gbServer = gbServerService.getOne(queryWrapper,false);

            // 所有设备和通道推送给 国标平台
            List<ChannelMsg> channelMsgs = new ArrayList<>();
            for(GbChannelBean gbChannelBean : gbChannelBeans){
                channelMsgs.clear();
                if (gbServer.getId().equals(gbChannelBean.getGbDeviceId())){
                    // 组装成传输的数据
                    ChannelMsg channelMsg = new ChannelMsg().setMethod(method)
                            .setChannelInfo(new ChannelInfo()
                                    .setChannelId(gbChannelBean.getChannelId())
                                    .setChannelName(gbChannelBean.getChannelName())
                                    .setOriginChannelId(gbChannelBean.getChannelId())
                                    .setGbDeviceId(gbChannelBean.getGbDeviceId())
                                    .setLongitude(gbChannelBean.getLongitude())
                                    .setLatitude(gbChannelBean.getLatitude())
                                    .setPtzType(gbChannelBean.getPtzType()));
                    channelMsgs.add(channelMsg);
                }
                // 通道列表发送到 国标平台
                if (channelMsgs.size() > 0){
                    // 通道列表发送到 国标平台
                    TransmitData transmitData1 = new TransmitData();
                    transmitData1.setType("CHANNELLIST");
                    transmitData1.setData(channelMsgs);
                    log.debug(JsonUtil.toJson(transmitData1));
                    try{
                        HttpUtil.doPost(
                                "http://" + gbServer.getHost() + ":" + gbServer.getInterfacePort() + "/api/data/from",
                                JsonUtil.toJson(transmitData1),
                                null,
                                30000
                        );
                    }catch (Exception e){

                    }
                }
            }
        }*/
    }
}
